//
//  STHeaderView.m
//  赛拓
//
//  Created by 王磊 on 7/29/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STHeaderView.h"
#import "STBannerView.h"
#import "STBrandView.h"
#import "STSubjectView.h"
#import "STBottomLineView.h"
#import "STHomeBean.h"
@interface STHeaderView()<STBaseViewDelegate>

@property (nonatomic ,strong) STBannerView *bannerView;

@property (nonatomic ,strong) NSMutableArray *bannerData;

@property (nonatomic ,strong) STBrandView *brandView;

@property (nonatomic ,strong) NSMutableArray *brandData;

@property (nonatomic ,strong) STSubjectView *subjectView;

@property (nonatomic ,strong) NSMutableArray *subjectData;

@property (nonatomic ,strong) STBottomLineView *hotGoods;
@end
@implementation STHeaderView
- (STBannerView *)bannerView {
    if (!_bannerView) {
        _bannerView = [STBannerView instance];
        
        _bannerView.mDelegate = self;
        
        [self addSubview:_bannerView];
    }
    return _bannerView;
}
- (NSMutableArray *)bannerData {
    if (!_bannerData) {
        _bannerData = [NSMutableArray array];
    }
    return _bannerData;
}
- (STBrandView *)brandView {
    if (!_brandView) {
        _brandView = [STBrandView instance];
        
        _brandView.mDelegate = self;
        
        [self addSubview:_brandView];
    }
    return _brandView;
}
- (NSMutableArray *)brandData {
    if (!_brandData) {
        _brandData = [NSMutableArray array];
    }
    return _brandData;
}

- (STSubjectView *)subjectView {
    if (!_subjectView) {
        _subjectView = [STSubjectView instance];
        
        _subjectView.mDelegate = self;
        
        [self addSubview:_subjectView];
    }
    return _subjectView;
}
- (NSMutableArray *)subjectData {
    if (!_subjectData) {
        _subjectData = [NSMutableArray array];
    }
    return _subjectData;
}

- (STBottomLineView *)hotGoods {
    if (!_hotGoods) {
        _hotGoods = [STBottomLineView instance];
        
        _hotGoods.alignment = NSTextAlignmentCenter;
        
        _hotGoods.textColor = ST_167EC0_COLOR;
        
        _hotGoods.font = [UIFont boldSystemFontOfSize:15];
        [self addSubview:_hotGoods];
    }
    return _hotGoods;
}
- (void)commitInit {
    
}
- (void)updateData:(id)data {
    
    STHomeBean *home = data;
    
    if (self.bannerData.count) {
        [self.bannerData removeAllObjects];
    }
    
    NSArray *banner = home.tads;
    
    if (banner.count) {
        
        [self.bannerData addObjectsFromArray:banner];
        
        [self.bannerView updateData:self.bannerData];
    }
    
    NSArray *brand = home.productors;
    
    NSArray *subject = home.cates;
    
    if (self.brandData.count) {
        [self.brandData removeAllObjects];
    }
    
    if (brand.count) {
        
        [self.brandData addObjectsFromArray:brand];
        
        [self.brandView updateData:self.brandData];
    }
    if (self.subjectData.count) {
        
        [self.subjectData removeAllObjects];
    }
    
    if (subject.count ) {
        
        [self.subjectData addObjectsFromArray:subject];
        
        [self.subjectView updateData:self.subjectData];
        
    }
    [self.hotGoods updateData:@"热门商品"];
}
#define margin 10
- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.bannerData.count) {
        
        self.bannerView.frame = CGRectMake(0, 0, ST_SCREEN_WIDTH, 320 * ST_RATE);
    } else {
        self.bannerView.frame = CGRectMake(0, 0, ST_SCREEN_WIDTH, 0);
    }
    if (self.subjectData.count) {
        
        self.subjectView.frame = CGRectMake(0, CGRectGetMaxY(self.bannerView.frame), ST_SCREEN_WIDTH, 350 * ST_RATE);
    } else {
        self.subjectView.frame = CGRectMake(0, CGRectGetMaxY(self.bannerView.frame), ST_SCREEN_WIDTH, 0);
    }
    if (self.brandData.count) {
        
        self.brandView.frame = CGRectMake(0, CGRectGetMaxY(self.subjectView.frame) + 20 * ST_RATE, ST_SCREEN_WIDTH, 200 * ST_RATE);
    } else {
        self.brandView.hidden = true;
        self.brandView.frame = CGRectMake(0, CGRectGetMaxY(self.subjectView.frame) + 20 * ST_RATE, ST_SCREEN_WIDTH, 0);
    }
    self.hotGoods.frame = CGRectMake(0, CGRectGetMaxY(self.brandView.frame), ST_SCREEN_WIDTH , 60 * ST_RATE);
}
+ (CGFloat)headerViewHeight:(id)data {
    STHomeBean *home = data;
    // 顶部轮播图
    NSArray *banner = home.tads;
    // 推荐品牌
    NSArray *brand = home.productors;
    
    NSArray *subject = home.cates;
    
    CGFloat height = 0;
    
    if (banner.count) {
        
        height += 320 * ST_RATE;
        
    }
    if (subject.count ) {
        
        height += 350 * ST_RATE;
        
        height += 60 * ST_RATE;
    }
    
    if (brand.count) {
        
        height += 20 * ST_RATE;
        
        height += 200 * ST_RATE;
        
    }
    return height;
}
- (void)onItemClick:(STItemClickType)itemType withData:(id)data forIndexPath:(NSIndexPath *)indexPath {
    
    if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(onItemClick:withData:forIndexPath:)]) {
        
        [self.mDelegate onItemClick:itemType withData:data forIndexPath:indexPath];
    }
}
@end
